home *** CD-ROM | disk | FTP | other *** search
/ Just Call Me Internet / Just Call Me Internet.iso / prog / atari / c / ltick03 / misc.c < prev    next >
C/C++ Source or Header  |  1993-07-06  |  14KB  |  754 lines

  1. /*
  2.  *    Misc functions for LazyTick/LazyFix
  3.  *    (LazyTick/LazyFix Project)
  4.  *   
  5.  *    Public Domain: may be copied and sold freely
  6.  */
  7.  
  8. #include    <stdio.h>
  9. #include    <stdlib.h>
  10. #include    <time.h>
  11. #include    <portab.h>
  12. #include    <ctype.h>
  13. #include    <string.h>
  14. #include    <stdarg.h>
  15.  
  16. #include    "qbbs.h"
  17. #include    "tick.h"
  18. #include     "misc.h"
  19. #include    "config.h"
  20.  
  21. #define timenix(a) time(a)
  22.  
  23. #define MSGSIZE 32000     /* Max message size */
  24. #define BUFSIZE 1024      /* Copy buffer */
  25. #define USEPID             /* define to use ^PID kludge */
  26. #define PIDSTR    LAZYNAME /* PID name */
  27. #define PIDVER  LAZYVERS /* PID version */
  28.  
  29. /* ================================================= FILE ECHOLIST */
  30. /* open_areas: load <path>fareas.bbs
  31.    close_areas: release buffers
  32.    get_area: return pointer to area field or NULL
  33.    debug_areas: print buffer */
  34.  
  35. ECHOLIST *el=NULL;
  36. int maxel=0;
  37.  
  38. void close_areas(void )
  39. {
  40.     if(el)
  41.         free(el);
  42. }
  43.  
  44. ECHOLIST *get_area(char *areatag)
  45. {
  46.     int i;
  47.     if(!el)
  48.         return NULL;
  49.     
  50.     for(i=0;i<maxel;i++)
  51.     {
  52.         if(stricmp(areatag,el[i].name)==0)
  53.             return (el+i);
  54.     }
  55.     return NULL;
  56. }
  57.  
  58. int debug_areas(void )
  59. {
  60.     int i;
  61.     
  62.     if(!el)
  63.         return BBSFAIL;
  64.     
  65.     printf("Debugging ECHOLIST\n");
  66.     
  67.     for(i=0;i<maxel;i++)
  68.     {
  69.         FIDONODE *currentnode;
  70.         
  71.         printf("Farea: %s; path: %s; group: %s; line: %d-%d\n",
  72.             el[i].name,el[i].file,el[i].group,el[i].line,el[i].modify);
  73.         printf(" desc: %s\n",el[i].desc);
  74.         printf(" nodes: ");
  75.         
  76.         currentnode=el[i].firstnode;
  77.         while(currentnode)
  78.         {
  79.             printf("[%d]%d:%d/%d.%d ",
  80.                 currentnode->position,currentnode->zone,
  81.                 currentnode->net,currentnode->node,
  82.                 currentnode->point);
  83.             currentnode=currentnode->next;
  84.         }
  85.         printf("\n");
  86.     }
  87.     return BBSOK;
  88. }
  89.  
  90. int open_areas(char *path)
  91. {
  92.     FILE *abbs;
  93.     char curgroup[BBSSTR];
  94.     char curdesc[BBSSTR];
  95.     char buffer[200];
  96.     int curarea=0;
  97.     char *ptr;
  98.     int tmpzone,tmpnet,tmpnode,tmppoint;
  99.     int curline=0;
  100.     FIDONODE *currentnode;
  101.     
  102.     el=malloc(sizeof(ECHOLIST)*MAXECHO+1);
  103.     if(!el)
  104.     {
  105.         logline(5,"Can't malloc echolist!");
  106.         the_end(242);
  107.     }
  108.     
  109.     strcpy(curgroup,"none");
  110.     strcpy(curdesc,"No description.");
  111.     
  112.     abbs=fopen(path,"r");
  113.     if(abbs)
  114.     {
  115.         if(fgets(buffer,199,abbs)!=NULL)
  116.         { /* skip origin line */    
  117.             while(fgets(buffer,199,abbs)!=NULL)
  118.             {
  119.                 curline++; /* current areas.bbs line number */
  120.                 
  121.                 strcln(buffer,-1);
  122.                 if(iscomment(*buffer))
  123.                     ;
  124.                 else if(buffer[0]=='-')
  125.                 {
  126.                     if(strnicmp(buffer,"-group",(size_t) strlen("-group"))==0)
  127.                     { /* got new flags */
  128.                         ptr=nextstr(buffer);
  129.                         if(ptr)
  130.                         {
  131.                             if(strlen(ptr)>8)
  132.                                 ptr[8]=0;
  133.                             strlwr(ptr);
  134.                             strspacecpy(curgroup,ptr);
  135.                         }
  136.                     }
  137.                     
  138.                     if(strnicmp(buffer,"-desc",(size_t) strlen("-desc"))==0)
  139.                     { /* got desc */
  140.                         ptr=nextstr(buffer);
  141.                         if(ptr)
  142.                         {
  143.                             if(strlen(ptr)>=BBSSTR-1)
  144.                                 ptr[BBSSTR-2]=0;
  145.                             strcpy(curdesc,ptr);
  146.                         }
  147.                     }                
  148.                 }
  149.                 else /* got an area */
  150.                 {
  151. #ifndef GET_PASSTHRU
  152.                     if(strnicmp(buffer,"passthru",strlen("passthru")))
  153. #endif
  154.                     { /* !passthru area */
  155.                         strspacecpy(el[curarea].file,buffer);
  156.                         ptr=nextstr(buffer);
  157.                         if(ptr)
  158.                         {
  159.                             el[curarea].modify=0;
  160.                             el[curarea].line=curline;
  161.                             strcpy(el[curarea].group,curgroup);
  162.                             strcpy(el[curarea].desc,curdesc);
  163.                             strspacecpy(el[curarea].name,ptr);
  164.  
  165.                             /* search node number list */
  166.                             el[curarea].firstnode=NULL;
  167.  
  168.                             while((ptr=nextstr(ptr))!=NULL)
  169.                             {
  170.                                 if(getaddress(ptr,&tmpzone,&tmpnet,&tmpnode,&tmppoint))
  171.                                 {
  172.                                     /* allocate new node */
  173.                                     if(el[curarea].firstnode==NULL)
  174.                                     {    
  175.                                         currentnode=el[curarea].firstnode=malloc(sizeof(FIDONODE));
  176.                                     }
  177.                                     else
  178.                                     {
  179.                                         currentnode->next=malloc(sizeof(FIDONODE));
  180.                                         currentnode=currentnode->next;
  181.                                     }
  182.                                     
  183.                                     /* check malloc */
  184.                                     if(!currentnode)
  185.                                     {
  186.                                         logline(5,"Malloc error while loading fareas.bbs");
  187.                                         the_end(242);
  188.                                     }
  189.                                     
  190.                                     currentnode->next=NULL;
  191.                                     currentnode->position=(int)(strlen(buffer)-strlen(ptr));
  192.                                     currentnode->zone=tmpzone;
  193.                                     currentnode->net=tmpnet;
  194.                                     currentnode->node=tmpnode;
  195.                                     currentnode->point=tmppoint;
  196.                                 }
  197.                                 else
  198.                                     logline(2,"Bad node number in fareas.bbs: %s",ptr);
  199.                             }
  200.  
  201.                             /* next area */
  202.                             if(curarea<(MAXECHO-2))
  203.                                 curarea++;
  204.                         }
  205.                     } /* passthru */
  206.                     /* reset desc */
  207.                     strcpy(curdesc,"No description.");
  208.                 } /* got */
  209.             }
  210.         }
  211.         fclose(abbs);    
  212.         el[curarea].file[0]=0; /* end of list marker */
  213.         
  214.     }
  215.     else
  216.         return BBSFAIL;
  217.         
  218.     maxel=curarea;
  219.     return BBSOK;
  220. }
  221.  
  222. /* ========================================================= FILE COPY */
  223.  
  224. int copyfile(char *one,char *two)
  225. {
  226.     FILE *onef;
  227.     FILE *twof;
  228.     unsigned char bufc;
  229.     int error=0;
  230.     
  231.     unsigned long count=0;
  232.     time_t timer;
  233.     char tdate[BBSSTR];
  234.     
  235.     onef=fopen(one,"rb");
  236.     if(!onef)
  237.     {
  238.         logline(4,"Can't open %s",one);
  239.         return BBSFAIL;
  240.     }
  241.     else
  242.     {
  243.         twof=fopen(two,"wb");
  244.         if(!twof)
  245.         {
  246.             logline(4,"Can't open %s",two);
  247.             error++;
  248.         }
  249.         else
  250.         {
  251.             time (&timer);
  252.               strftime (tdate, 20, "%d %b %H:%M:%S", localtime(&timer));
  253.               printf("$ %s %s Copying",tdate,LAZYLGID);
  254.             while(fread(&bufc,1,1,onef)==1)
  255.             {
  256.                 fwrite(&bufc,1,1,twof);
  257.                 if(((count++)%25000)==0)
  258.                     printf(".");
  259.             }
  260.             printf("\n");
  261.               
  262.             if(ferror(twof))
  263.             {
  264.                 logline(4,"Error while processing %s",two);
  265.                 error++;
  266.             }
  267.             fclose(twof);
  268.         }
  269.         
  270.         if(ferror(onef))
  271.         {
  272.             logline(4,"Error while processing %s",one);
  273.             error++;
  274.         }
  275.         fclose(onef);
  276.     }
  277.     
  278.     if(error)
  279.         return BBSFAIL;
  280.     return BBSOK;
  281. }
  282.  
  283. /* ====================================================== POST MESSAGE */
  284. /* postmsg: post a mail msg */
  285.  
  286. int postmsg(char *from, char *fromaddr, char *to, char *toaddr, char *subject, 
  287.                 char *arearad, char *msgtext, int private)
  288. {
  289.     FILE *hdr,*msg,*txt;
  290.     int i,error;
  291.     time_t curtm;
  292.     size_t max;
  293.     long offset,msgsize;
  294.     int dzone=0,dnet=0,dnode=0,dpoint=0;
  295.     int ozone=0,onet=0,onode=0,opoint=0;
  296.     char *zebuffer;
  297.     
  298.     char areahdr[FILENAME_MAX];
  299.     char areamsg[FILENAME_MAX];
  300.     char temp[BBSSTR];
  301.     char zerotemp=0;
  302.     QBBS_MHEADER ourmsg;
  303.     FIDONODE tmpnode;
  304.     
  305.     zebuffer=malloc(BUFSIZE+1);
  306.     if(!zebuffer)
  307.     {
  308.         logline(5,"Can't malloc postmsg buffer");
  309.         the_end(242);
  310.     }
  311.     
  312.     /* convert addresses */
  313.     if(toaddr)
  314.         sscanf(toaddr,"%d:%d/%d.%d",&dzone,&dnet,&dnode,&dpoint);
  315.     else
  316.     {
  317.         get_ouraddress(&tmpnode,0,0,0,0);
  318.         dzone=tmpnode.zone;
  319.         dnet=tmpnode.net,
  320.         dnode=tmpnode.node;
  321.         dpoint=tmpnode.point;
  322.     }
  323.     if(fromaddr)
  324.         sscanf(fromaddr,"%d:%d/%d.%d",&ozone,&onet,&onode,&opoint);
  325.     else
  326.     {
  327.         get_ouraddress(&tmpnode,dzone,dnet,dnode,dpoint);
  328.         ozone=tmpnode.zone;
  329.         onet=tmpnode.net,
  330.         onode=tmpnode.node;
  331.         opoint=tmpnode.point;
  332.     }
  333.     
  334.     /* create area datafiles names */
  335.     strcpy(areahdr,arearad);
  336.     strcat(areahdr,".HDR");
  337.     strcpy(areamsg,arearad);
  338.     strcat(areamsg,".MSG");
  339.     
  340.     time(&curtm);
  341.     msgsize=0;
  342.     error=0;
  343.     offset=0;
  344.     
  345.     /* old .msg size */
  346.     msg=fopen(areamsg,"rb");
  347.     if(msg)
  348.     {
  349.         fseek(msg,0,SEEK_END);
  350.         offset=ftell(msg);
  351.         if(offset<0)
  352.             offset=0;
  353.         fclose(msg);
  354.     }
  355.             
  356.     /* we gonna write the text in the .msg file */
  357.     msg=fopen(areamsg,"ab");
  358.     if(msg)
  359.     {
  360.         time_t id;
  361.         time(&id);
  362.         
  363.         /* Write MSGID kludge */
  364.         if(opoint==0)
  365.             sprintf(temp,"\01MSGID: %d:%d/%d %8.8lx\x0a",
  366.                 ozone,onet,onode,(long)id);
  367.         else
  368.             sprintf(temp,"\01MSGID: %d:%d/%d.%d %8.8lx\x0a",
  369.                 ozone,onet,onode,opoint,(long)id);
  370.         
  371.         if(fwrite(temp,(size_t) strlen(temp),1,msg)==1)
  372.         {
  373.             msgsize+=strlen(temp);
  374.             
  375.             /* Write PID kludge */
  376.             #ifdef USEPID
  377.             sprintf(temp,"\01PID: %s %s\x0a",PIDSTR,PIDVER);
  378.             if(fwrite(temp,(size_t) strlen(temp),1,msg)==1)
  379.                 msgsize+=strlen(temp);
  380.             else 
  381.                 error++;
  382.             #endif
  383.                 
  384.             /* Read the message itself and add to MSG */
  385.             txt=fopen(msgtext,"r");
  386.             if(txt)
  387.             {
  388.                 error=0;
  389.                 do
  390.                    {
  391.                        max=fread(zebuffer,1,BUFSIZE,txt);
  392.                        if(max>0)
  393.                        {
  394.                         msgsize+=max;
  395.                         if(fwrite(zebuffer,1,max,msg)!=max)
  396.                             error++;
  397.                     }
  398.                     else if(max<0)
  399.                         error++;
  400.                     /* copy only the first MSGSIZE bytes */
  401.                    } while((max==BUFSIZE) && (error==0) && msgsize<MSGSIZE);
  402.                 
  403.                 fclose(txt);
  404.                 
  405.                 /* Write Tearline */
  406. #ifdef USETEAR
  407.                 if(!private)
  408.                 {
  409.                     sprintf(temp,"--- %s %s\x0a",PROGNAME,VERSTR);
  410.                     if(fwrite(temp,(size_t) strlen(temp),1,msg)!=1)
  411.                         error++;
  412.                     msgsize+=strlen(temp);
  413.                 }
  414. #endif
  415. #ifndef DONTUSEZERO
  416.                 zerotemp=0;
  417.                 if(fwrite(&zerotemp,(size_t)1,1,msg)!=1)
  418.                         error++;
  419.                 msgsize+=1;
  420. #endif
  421.             }
  422.             else
  423.                 error++;                            
  424.         }
  425.         else
  426.             error++;
  427.         
  428.         
  429.         fclose(msg);
  430.     }
  431.     else
  432.         error++;
  433.         
  434.     if(!error)
  435.     {
  436.         /* Init header */
  437.         strcpy(ourmsg.from,from);
  438.         strcpy(ourmsg.to,to);
  439.         strcpy(ourmsg.subject,subject);
  440.         strftime(ourmsg.time,20,"%d %b %y  %H:%M:%S",localtime(&curtm));
  441.         
  442.         ourmsg.stamp=timenix(NULL); 
  443.                     /* fixme? number of seconds since 1/1/70 */
  444.     
  445.         ourmsg.reserved1=0; /* ? */
  446.         ourmsg.reply_to=0; /* set by burep, etc */
  447.         ourmsg.attrib=POSTFLAG;
  448.  
  449.         if(private)
  450.             ourmsg.attrib|=PRVTFLAG;
  451.                     
  452.         for(i=0;i<8;i++)
  453.             ourmsg.mailer[i]=0;
  454.         
  455.         /* Set size and offset */
  456.         ourmsg.size=(UWORD) msgsize;
  457.         ourmsg.msgoffset=offset;
  458.     
  459.         
  460.         ourmsg.read_count=0;
  461.         ourmsg.cost=0;
  462.         ourmsg.o_zone=(WORD) ozone;
  463.         ourmsg.o_net=(WORD) onet;
  464.         ourmsg.o_node=(WORD) onode;
  465.         ourmsg.o_point=(WORD) opoint;
  466.         
  467.         if(dzone)
  468.         {
  469.             ourmsg.d_zone=(WORD) dzone;
  470.             ourmsg.d_net=(WORD) dnet;
  471.             ourmsg.d_node=(WORD) dnode;
  472.             ourmsg.d_point=(WORD) dpoint;
  473.         }
  474.         else
  475.         {
  476.             ourmsg.d_zone=0; 
  477.             ourmsg.d_net=0; 
  478.             ourmsg.d_node=0; 
  479.             ourmsg.d_point=0;
  480.         }
  481.     
  482.         /* And write header */    
  483.         hdr=fopen(areahdr,"ab");
  484.         if(hdr)
  485.         {
  486.             if(fwrite(&ourmsg,sizeof(QBBS_MHEADER),1,hdr)!=1)
  487.                 error++;
  488.             fclose(hdr);
  489.         }
  490.         else 
  491.             error++;
  492.     }
  493.     
  494.     free(zebuffer);
  495.     
  496.     if(error)
  497.     {
  498.         logline(4,"Error while trying to post message");
  499.         return BBSFAIL;
  500.     }
  501.     
  502.     return BBSOK;
  503. }
  504.  
  505. /* ==================================================== EMERGENCY EXIT */
  506.  
  507. void the_end(int exitn)
  508. {
  509.     logline(3,"Exiting LazyTick");
  510.     close_areas();
  511.     close_the_log();
  512.     exit(exitn);
  513. }
  514.  
  515. /* =================================================== STRING PARSING */
  516.  
  517. /*
  518.  * Insert one space at the beginning of a string
  519.  */
  520.  
  521. void strspins(char *str)
  522. {
  523.     int maxlen,i;
  524.     
  525.     maxlen=strlen(str);
  526.     
  527.     for(i=maxlen+1;i>=0;i--)
  528.         str[i+1]=str[i];
  529.     str[0]=' ';
  530. }
  531.  
  532. /*
  533.  * fgets who don't stop when string is full 
  534.  */
  535.  
  536. char *fticgets(char *str, size_t len, FILE *fl)
  537. {
  538.     int idx=0,mychar;
  539.     
  540.     while(!feof(fl))
  541.     {
  542.         if(EOF==(mychar=fgetc(fl)))
  543.             break;
  544.             
  545.         if(idx<(len-1))
  546.             str[idx++]=(char) mychar;    
  547.         
  548.         if(mychar=='\n')
  549.             break;
  550.     }
  551.     str[idx]=0;
  552.     
  553.     if(idx)
  554.         return str;
  555.  
  556.     return NULL;
  557. }
  558.  
  559. /* 
  560.  * case insensitive comparison of string ENDINGS 
  561.  */
  562.  
  563. int strendcmp(char *one, char *two)
  564. {
  565.     if(strlen(one)>strlen(two))
  566.         return stricmp(one+(strlen(one)-strlen(two)),two);
  567.     else
  568.         return stricmp(two+(strlen(two)-strlen(one)),one);
  569. }
  570.  
  571. /*
  572.  * xtol hexa to int
  573.  */
  574.  
  575. unsigned long xtol(char *str)
  576. {
  577.     unsigned long temp;
  578.     
  579.     temp = 0;
  580.     while(*str && isalnum(*str))
  581.     {
  582.         temp *= 16;
  583.         temp += isdigit(*str) ? *str-'0' : toupper(*str)-'A'+10;
  584.         str++;
  585.     }
  586.     return temp;
  587. }
  588.  
  589. /*
  590.  *    like nextstr but skip first ':'
  591.  */
  592.  
  593. char *nextstrtic(char *str)
  594. {
  595.     if(*str==':')
  596.         str++;
  597.     return nextstr(str);
  598. }
  599.  
  600. /*
  601.  *    for tic file processing
  602.  */
  603.  
  604. int strticcmp(char *one, char *key)
  605. {
  606.     return strnicmp(one,key,strlen(key));
  607. }
  608.  
  609. /*
  610.  *    file function that makes a path from ambigous string
  611.  */
  612.  
  613. void addslash(char *s)
  614. {
  615.     if(*s)
  616.     {
  617.         if(s[strlen(s)-1]!=SYSSEPAR)
  618.             strcat(s,SYSSTRSEPAR);
  619.     }
  620. }
  621.  
  622. /*
  623.  *    iscomment: is this char a comment?
  624.  */
  625.  
  626. int iscomment(char c)
  627. {
  628.     switch(c)
  629.     {
  630.         case '#':
  631.         case ';':
  632.         return 1;
  633.     }
  634.     return 0;
  635. }
  636.  
  637. /*
  638.  *    strcln: remove a char from string
  639.  */
  640.  
  641. void strcln(char *string, char c)
  642. {
  643.     char *s=string;
  644.     while(*s)
  645.     {
  646.         if((c!=-1 && *s==c) || (c==-1 && *s<0x20 && *s>0))
  647.             strcpy(s,s+1);
  648.         s++;
  649.     }
  650. }
  651.  
  652. /* find next string format "plouf <tab>tralala ; comment */
  653.  
  654. char *nextstr(char *str)
  655. {
  656.     char* s=str;
  657.     
  658.     while((*s!='\0') && (*s!=' ') && (*s!='\x09'))
  659.         s++;
  660.         
  661.     if(*s=='\0')
  662.         return NULL;
  663.  
  664.     while((*s==' ') || (*s=='\x09'))
  665.         s++;
  666.     
  667.     if((*s=='\0') || (*s==';'))
  668.         return NULL;
  669.         
  670.     return s;
  671. }
  672.  
  673. /* copy string until space */
  674.  
  675. void strspacecpy(char *dest, char *srce)
  676. {
  677.     int i=0,j=0;
  678.     
  679.     while((srce[i]!='\0') && (srce[i]!=' ') && (srce[i]!='\x09') && (i<BBSSTR))
  680.     {
  681. /* fixme: unsigned  char ? */
  682.         if(srce[i]>' ')
  683.         {
  684.             dest[j]=srce[i];
  685.             j++;
  686.         }
  687.         i++;
  688.     } 
  689.     dest[i]='\0';
  690. }
  691.  
  692. /* ================================================= LOG FILE FUNCTIONS */
  693. /* open_the_log: open logfile <name> with <level>
  694.    close_the_log: close logfile 
  695.    logline: log line with <level>, printf syntax */
  696.  
  697. FILE *logfile=NULL;
  698. int loglevel=0;
  699.  
  700. /* logline: Log a line in the logfile and output to screen */
  701.  
  702. void logline (int level, char *line, ...)
  703. {
  704.     time_t    timer;
  705.     char    temp[2*BBSSTR], out[2*BBSSTR],tdate[20];
  706.     va_list param;
  707.     struct tm *tim;
  708.     
  709.     char ls[]=" -+*=!";
  710.         
  711.     if(level>5) level=5;
  712.     if(level<0) level=0;
  713.     
  714.     /* process */
  715.     va_start(param, line);
  716.     vsprintf(temp, line, param);
  717.     va_end(param);
  718.     time (&timer);
  719.     tim = localtime (&timer);
  720.     strftime (tdate, 20, "%d %b %H:%M:%S", tim);
  721.     sprintf (out, "%c %s %s %s\n", ls[level], tdate, LAZYLGID, temp);
  722.  
  723.     /* log to file */
  724.     if(logfile && level>0)
  725.     {
  726.         fputs(out,logfile);
  727. #ifdef DEBUG
  728.         fflush(logfile);
  729. #endif
  730.     }
  731.     printf(out);
  732. }
  733.  
  734. /* openlog: create logfile */
  735.  
  736. int open_the_log(char *nm, int level)
  737. {
  738.     loglevel=level;
  739.     logfile=fopen(nm,"a");
  740.     if(logfile)
  741.         return BBSOK;
  742.     
  743.     printf("Can't open logfile!\n");
  744.     return BBSFAIL;
  745. }
  746.  
  747. /* closelog: close logfile */
  748.  
  749. void close_the_log(void )
  750. {
  751.     if(logfile)
  752.         fclose(logfile);
  753. }
  754.